package com.way.designpatterncodepratice.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author liangliwei 80551025
 * @description cglib代理
 * @date 2020-06-19
 */
public class CgLibProxy implements MethodInterceptor {

    private Enhancer enhancer = new Enhancer();

    @Override
    public Object intercept(Object target, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib代理开始");
        Object result = methodProxy.invokeSuper(target, objects);
        System.out.println("cglib代理结束");
        return result;
    }

    public Object newProxyInstance(Class<?> c){
        enhancer.setSuperclass(c);
        enhancer.setCallback(this);
        return enhancer.create();
    }
}
